<?php
/**
 * RedCross Application
 *
 * @copyright 2011
 */

/**
 * MembershipFee gateway class for communicating with persistent storage
 */
class Core_Gateway_MembershipFee extends RedCross_Gateway_GatewayAbstract
{
    /**
     * Constructor
     * Defines some private variables
     */
    public function __construct()
    {
        $this->_class = 'MembershipFee';
        $this->_modelPath = 'Core_Model';
    }

    public function fetchByMemberIdAndYear($memberId, $year) {
        $query = Doctrine_Query::create()
            ->select('*')
            ->from('Core_Model_MembershipFee mf')
            ->where('year = ?', $year)
            ->andWhere('member_id = ?', $memberId);
        return $query->execute();
    }

    public function fetchYears() {
        // SELECT DISTINCT year FROM redcross_membership_fee ORDER BY year DESC
        $query = Doctrine_Query::create()
            ->select('DISTINCT year as year, COUNT(DISTINCT member_id) as num_members')
            ->from('Core_Model_MembershipFee mf')
            ->groupBy('year')
            ->orderBy('year DESC');
        return $query->fetchArray();
    }

    public function fetchPaidMembersForYear($year) {
        $query = Doctrine_Query::create()
            ->select('m.first_name, m.last_name, mf.year')
            ->from('Core_Model_Member m')
            ->leftJoin('m.MembershipFees mf')
            ->where('mf.year = ?', $year);
        return $query->execute();
    }
}

?>
